<?php
namespace App\Services;
use App\Models\Workcorp;
use Log;

class WorkdataService
{
    public static function workCorpInfo($bussinessInfo,$users_id,$suite_id)
    {
        // 处理授权,刷新后的企业数据
        // 授权方企业微信id
        $corpid = isset($bussinessInfo["auth_corp_info"]["corpid"]) ? $bussinessInfo["auth_corp_info"]["corpid"] : "";
        // 授权方企业微信名称
        $corp_name = isset($bussinessInfo["auth_corp_info"]["corp_name"]) ? $bussinessInfo["auth_corp_info"]["corp_name"] : "";
        $verified_end_time = isset($bussinessInfo["auth_corp_info"]["verified_end_time"]) ? $bussinessInfo["auth_corp_info"]["verified_end_time"] : "";
        // 授权方企业微信方形头像
        $square_logo_url = isset($bussinessInfo["auth_corp_info"]["corp_square_logo_url"]) ? $bussinessInfo["auth_corp_info"]["corp_square_logo_url"] : "";
        $agentid = isset($bussinessInfo["auth_info"]["agent"][0]["agentid"]) ? $bussinessInfo["auth_info"]["agent"][0]["agentid"] : "";
        $workCorp = Workcorp::getWorkCorpDetialId($corpid);

        if (!empty($workCorp)) {
            // 企业微信永久授权码,刷新没有本数据
            $permanent_code = isset($bussinessInfo["permanent_code"]) ? $bussinessInfo["permanent_code"] : $workCorp->permanent_code;
            // 更新企业授权吗
            Workcorp::workCorpRebind($corpid,$permanent_code,$corp_name,$square_logo_url,$agentid,$verified_end_time);
        } else {
            // 企业微信永久授权码,最长为512字节
            $permanent_code = $bussinessInfo["permanent_code"];
            $kf_corps_id = Workcorp::workCorpCreate($users_id,$corpid,$corp_name,$square_logo_url,$permanent_code,$suite_id,$agentid,$verified_end_time);
        }
    }
}
